#include "../../../vcd-common-api/vcd-common.h"
#include "../../vcd-message-dialog.h"
#include "vcd-wifi.h"

GtkWidget *g_btn_wifi_start;
GtkWidget *g_wifi_spinner;
int g_timeout_id_wifi_connect = 0;
int g_wifi_connect_time = 0;
int g_whether_wifi = 0;
gchar *g_text_wifi_name, *g_text_wifi_passwd;
GtkTreeModel *g_items_model;
GtkWidget *g_wifi_name_now;
GtkWidget *g_treeview_wifi;
GtkWidget *g_btn_wifi_research;
GtkWidget *g_btn_wifi_connect;
GtkWidget *g_btn_wifi_disconnect;
GtkWidget *g_btn_delete_connection_show;
GtkCellRenderer *g_renderer_name;
GtkWidget *g_sys_ask_window;

void get_wifi_passwd_ui(void);

typedef struct
{
    char name[100];
    char signal[5];
    char state[20];
}
Item;

gboolean get_whether_have_wifi_model(void)
{
#ifndef __linux__
    return FALSE;
#endif
    char tmp_buf[30];
    //char *cmd_buf = "lsmod|grep cfg80211|awk {'print $1'}";
    const char *cmd_buf = "lsmod |awk '$1~/^cfg80211$/{print $1}'";
    FILE *pf = popen(cmd_buf, "r");
    if (!fgets(tmp_buf, 30, pf)) {
        return FALSE;
    }
    pclose(pf);
    if (tmp_buf == NULL) {
        return FALSE;
    }
    else {
        return TRUE;
    }
}

int get_wifi_now(char *buf_wifi_status_now)
{
#ifndef __linux__
    return CLOSE;
#endif
    if (!g_whether_wifi) {
        strcpy(buf_wifi_status_now, "未开启");
        return CLOSE;
    }
    FILE *pf = popen("nmcli device status|grep wlan0|awk '{print $3}'", "r");
    fgets(buf_wifi_status_now, 50, pf);
    buf_wifi_status_now[strlen(buf_wifi_status_now) - 1] = 0;
    pclose(pf);
    if (strcmp(buf_wifi_status_now, "disconnected") == 0) {
        strcpy(buf_wifi_status_now, "未连接");
        return DISCONNECT;
    } else if (strcmp(buf_wifi_status_now, "connecting") == 0)
    {
        strcpy(buf_wifi_status_now, "正在连接...");
        return CONNECTING;
    } else
    {
        memset(buf_wifi_status_now, 0, 50);
        FILE *fp = popen("nmcli device status|grep wlan0|awk '{print $4}'", "r");
        fgets(buf_wifi_status_now, 50, fp);
        buf_wifi_status_now[strlen(buf_wifi_status_now) - 1] = 0;
        pclose(fp);
        return CONNECTED;
    }
}

int get_wifi_num(void)
{
    char wifi_num[5];
    FILE *pf_wifi_num = popen("nmcli device wifi list|awk 'NR>1'|wc -l", "r");
    fgets(wifi_num, sizeof(wifi_num), pf_wifi_num);
    wifi_num[strlen(wifi_num) - 1] = 0;
    pclose(pf_wifi_num);
    return atoi(wifi_num);
}

void get_connect_information(char *wifi_info)
{
    strcpy(wifi_info, "设备：              wlan0\n"
           "类型：              wifi\n"
           "硬件：              ");

    char hwaddr[20];
    FILE *pf_connect_info_hwaddr = popen("nmcli device show wlan0|grep GENERAL.HWADDR|awk '{print $2}'", "r");
    if (!fgets(hwaddr, sizeof(hwaddr), pf_connect_info_hwaddr))
        strcat(wifi_info, "\n");
    else
        strcat(wifi_info, hwaddr);
    pclose(pf_connect_info_hwaddr);

    char address[24];
    strcat(wifi_info, "地址：              ");
    FILE *pf_connect_info_address = popen("nmcli device show wlan0|grep IP4.ADDRESS|awk '{print $2}'", "r");
    if (!fgets(address, sizeof(address), pf_connect_info_address))
        strcat(wifi_info, "\n");
    else
        strcat(wifi_info, address);
    pclose(pf_connect_info_address);

    char gateway[20];
    strcat(wifi_info, "网关：              ");
    FILE *pf_connect_info_gateway = popen("nmcli device show wlan0|grep IP4.GATEWAY|awk '{print $2}'", "r");
    if (!fgets(gateway, sizeof(gateway), pf_connect_info_gateway))
        strcat(wifi_info, "\n");
    else
        strcat(wifi_info, gateway);
    pclose(pf_connect_info_gateway);

    char dns[20];
    strcat(wifi_info, "dns：               ");
    FILE *pf_connect_info_dns = popen("nmcli device show wlan0|grep IP4.DNS|awk '{print $2}'", "r");
    if (!fgets(dns, sizeof(dns), pf_connect_info_dns))
        ;
    else {
        dns[strlen(dns) - 1] = 0;
        strcat(wifi_info, dns);
    }
    pclose(pf_connect_info_dns);
}

gboolean set_wifi_connect_label(void)
{
    char buf_wifi_status_now[50], buf_connect_information[2048];
    int wifi_state = get_wifi_now(buf_wifi_status_now);
    get_connect_information(buf_connect_information);
    gtk_label_set_text((GtkLabel *)g_wifi_name_now, buf_wifi_status_now);
    gtk_widget_set_tooltip_text(g_wifi_name_now, buf_connect_information);
    if (wifi_state == CONNECTED) {
        if (g_timeout_id_wifi_connect) {
            g_source_remove(g_timeout_id_wifi_connect);
            g_timeout_id_wifi_connect = 0;
        }
        gtk_spinner_stop((GtkSpinner *)g_wifi_spinner);
        if (g_wifi_connect_time > 0)
            message_box("  连接成功!  ");
        g_wifi_connect_time = 0;
        return FALSE;
    }
    else {
        M_DEBUG("wifi connect times %d", g_wifi_connect_time);
        if (g_wifi_connect_time != 0)
            g_wifi_connect_time ++;
        if (g_wifi_connect_time == 20)
        {
            gtk_label_set_text((GtkLabel *)g_wifi_name_now, "未连接");
            system("nmcli device disconnect wlan0");
            g_source_remove(g_timeout_id_wifi_connect);
            g_timeout_id_wifi_connect = 0;
            gtk_spinner_stop((GtkSpinner *)g_wifi_spinner);
            g_wifi_connect_time = 0;
            message_box("  连接超时!  ");
            return FALSE;
        }
        return TRUE;
    }
}

void buf_all_get_wifi_name(char buf_all[], char buf_name[])
{
    char all[500];
    strcpy(all, buf_all);
    int i = strlen(all);
    for (; i > 0; i--)
    {
        if (all[i] == 'I' && all[i + 1] == 'n' && all[i + 2] == 'f' && all[i + 3] == 'r' && all[i + 4] == 'a')
        {
            break;
        }
    }
    all[i] = 0;
    M_DEBUG("i=%d, all=%s", i, all);
    i--;
    while (all[i] == ' ')
    {
        i--;
    }
    if (i > 0)
    {
        all[i + 1] = 0;
        M_DEBUG("i=%d, all=%s", i, all);
        strcpy(buf_name, ltrim(all));
        
        M_DEBUG(buf_name);
    }
}

void buf_all_get_wifi_signal(char buf_all[], char buf_signal[])
{
    char all[500];
    strcpy(all, buf_all);
    int i = strlen(all);
    for (; i > 0; i--)
    {
        if (all[i] == 'b' && all[i + 1] == 'i' && all[i + 2] == 't' && all[i + 3] == '/' && all[i + 4] == 's')
        {
            break;
        }
    }

    if (i > 0)
    {
        strncpy(buf_signal, all + i + 7, 4);
        buf_signal[4] = 0;
    }
}

void buf_all_get_wifi_state(char buf_all[], char buf_state[])
{
    char all[500];
    strcpy(all, buf_all);
    all[strlen(all) - 1] = 0;
    int i = strlen(all) - 10;
    strcpy(buf_state, all + i);
}

void add_items(void)
{
    if (g_wifi_connect_time == 0 || g_timeout_id_wifi_connect == 0)
    {
        gtk_list_store_clear(GTK_LIST_STORE(g_items_model));

        set_wifi_connect_label();
        GtkTreeIter iter;
        Item foo;
        int i;
        int wifi_num = get_wifi_num();
        M_DEBUG("wifi_num:%d", wifi_num);
        char buf_all[500];
        //char cmd[200] = "nmcli device wifi list|cut -b 4-|awk 'NR>1{printf \"%s\\n%s\\n%s\\n\", $1, $6, $8}'";
        char cmd[200] = "nmcli device wifi list|cut -b 4-|awk 'NR>1'";
        FILE *pf_wifi_list = popen(cmd, "r");
        for (i = 0; i < wifi_num; i++)
        {
            fgets(buf_all, sizeof(buf_all), pf_wifi_list);

            buf_all_get_wifi_name(buf_all, foo.name);
            buf_all_get_wifi_signal(buf_all, foo.signal);
            buf_all_get_wifi_state(buf_all, foo.state);

            gtk_list_store_append(GTK_LIST_STORE(g_items_model), &iter);
            gtk_list_store_set(GTK_LIST_STORE(g_items_model), &iter, 0, foo.name,
                               1, foo.signal,
                               2, foo.state,
                               -1);
            memset(foo.name,   0, 100);
            memset(foo.signal, 0, 5);
            memset(foo.state,  0, 20);
        }
        pclose(pf_wifi_list);
    }
}

GtkTreeModel *create_item_model(void)
{
    GtkListStore *model;

    model = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
    return (GtkTreeModel *)model;
}

void add_columns(GtkTreeView *g_treeview_wifi, GtkTreeModel *g_items_model)
{
    GtkCellRenderer *renderer;

    g_renderer_name = gtk_cell_renderer_text_new ();
    gtk_tree_view_insert_column_with_attributes ((GtkTreeView *)g_treeview_wifi,
            -1, "名称", g_renderer_name,
            "text", 0,
            NULL);

    renderer = gtk_cell_renderer_text_new ();
    gtk_tree_view_insert_column_with_attributes ((GtkTreeView *)g_treeview_wifi,
            -1, "信号强度", renderer,
            "text", 1,
            NULL);

    renderer = gtk_cell_renderer_text_new ();
    gtk_tree_view_insert_column_with_attributes ((GtkTreeView *)g_treeview_wifi,
            -1, "安全", renderer,
            "text", 2,
            NULL);
}

GtkWidget *create_g_treeview_wifi(void)
{
    g_items_model = create_item_model();
    g_treeview_wifi = gtk_tree_view_new_with_model(g_items_model);
    gtk_tree_selection_set_mode (gtk_tree_view_get_selection((GtkTreeView *)g_treeview_wifi),
                                 GTK_SELECTION_SINGLE);
    gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (g_treeview_wifi), TRUE);
    add_columns((GtkTreeView *)g_treeview_wifi, g_items_model);
    return g_treeview_wifi;
}

void set_wifi_state(void)
{
    char buf_wifi_state[50];
    memset(buf_wifi_state,0, sizeof(buf_wifi_state));
    FILE *pf_wifi_state = popen("nmcli device status|grep wlan0|awk '{print $3}'", "r");
    fgets(buf_wifi_state, 50, pf_wifi_state);
    buf_wifi_state[strlen(buf_wifi_state) - 1] = 0;
    pclose(pf_wifi_state);
    if (strcmp(buf_wifi_state, "unavailable") == 0) {
        g_whether_wifi = 0;
    }
    else {
        g_whether_wifi = 1;
    }
}

void wifi_disconnect(void)
{
    if (!g_whether_wifi) {
        message_box("   wifi未打开！   ");
        return;
    }
    system("nmcli device disconnect wlan0");
    if (g_timeout_id_wifi_connect) {
        g_source_remove(g_timeout_id_wifi_connect);
        g_timeout_id_wifi_connect = 0;
    }
    gtk_label_set_text((GtkLabel *)g_wifi_name_now, "未连接");
    gtk_spinner_stop((GtkSpinner *)g_wifi_spinner);
    g_wifi_connect_time = 0;
}

void get_wifi_passwd_ui(void)
{
    GtkWidget *fix, *label, *entry, *btnyes, *btnno;
    g_sys_ask_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_keep_above(GTK_WINDOW(g_sys_ask_window), TRUE);
    gtk_window_set_modal((GtkWindow *)g_sys_ask_window, TRUE);
    gtk_window_set_position( GTK_WINDOW(g_sys_ask_window), GTK_WIN_POS_CENTER_ALWAYS);//居中
    gtk_container_set_border_width(GTK_CONTAINER(g_sys_ask_window), 25);
    gtk_window_set_decorated(GTK_WINDOW(g_sys_ask_window), FALSE);//无边框

    btnyes = gtk_button_new_with_label("确定");
    btnno  = gtk_button_new_with_label("取消");
    label  = gtk_label_new("请输入密码:");
    fix    = gtk_fixed_new();
    entry  = gtk_entry_new();

    gtk_button_set_relief(GTK_BUTTON(btnno),  GTK_RELIEF_NONE);
    gtk_button_set_relief(GTK_BUTTON(btnyes), GTK_RELIEF_NONE);
    gtk_widget_set_size_request(btnyes, 100, -1);
    gtk_widget_set_size_request(btnno,  100, -1);
    gtk_widget_set_size_request(entry,  210, -1);
    gtk_entry_set_visibility((GtkEntry *)entry, FALSE);

    gtk_fixed_put((GtkFixed *)fix, label,  0,   0);
    gtk_fixed_put((GtkFixed *)fix, entry,  0,   20);
    gtk_fixed_put((GtkFixed *)fix, btnyes, 0,   60);
    gtk_fixed_put((GtkFixed *)fix, btnno,  110, 60);

    gtk_container_add(GTK_CONTAINER(g_sys_ask_window), fix);
    g_signal_connect_swapped(G_OBJECT(btnno),  "clicked", G_CALLBACK(gtk_widget_destroy), g_sys_ask_window);

    g_signal_connect_swapped(G_OBJECT(btnyes), "clicked", G_CALLBACK(get_wifi_passwd), entry);
    g_signal_connect(G_OBJECT(entry), "activate", G_CALLBACK(get_wifi_passwd), NULL);

    /*            CSS                  */
    set_css(btnyes, "button{color: white;background: rgb(53, 126, 169);border: 1px solid rgb(40, 94, 142);border-radius: 0px}");
    set_css(btnyes, "button:hover{border-radius: 0px;background: #3070a9}");
    set_css(btnno, "button{color: white;background: #5cb85c;border: 1px solid rgb(57, 132, 57);border-radius: 0px}");
    set_css(btnno, "button:hover{border-radius:0px;background: #449d44;}");
    set_css(g_sys_ask_window, "window{border-radius:0px;background-color: rgb(245, 245, 245)}");
    /*            CSS                 */
    gtk_widget_show_all(g_sys_ask_window);
    gtk_window_set_position( GTK_WINDOW(g_sys_ask_window), GTK_WIN_POS_CENTER_ALWAYS);//居中
}

void wifi_connect(void)
{
    #ifndef __WIN32__
    if (!g_whether_wifi) {
        message_box("   wifi未打开！   ");
        return;
    }
    GtkTreeIter iter;
    GtkTreeModel *model = gtk_tree_view_get_model((GtkTreeView *)g_treeview_wifi);
    GtkTreeSelection *selection = gtk_tree_view_get_selection((GtkTreeView *)g_treeview_wifi);
    if (gtk_tree_selection_get_selected(selection, NULL, &iter))
    {
        gtk_tree_model_get(model, &iter, 0, &g_text_wifi_name, -1);
        gtk_tree_model_get(model, &iter, 2, &g_text_wifi_passwd, -1);
        if (strcmp(g_text_wifi_passwd, "--") == 0)
        {
            gtk_spinner_start((GtkSpinner *)g_wifi_spinner);
            pid_t pid = fork();
            if (pid == 0)
            {
                M_DEBUG("execl...........");
                if (execl("/usr/bin/nmcli", "nmcli", "device", "wifi", "connect", g_text_wifi_name, NULL) < 0) {
                    exit(0);
                }
            }
            g_wifi_connect_time ++;
            g_timeout_id_wifi_connect = g_timeout_add_seconds(3, (GSourceFunc)set_wifi_connect_label, NULL);
        } else {
            get_wifi_passwd_ui();
        }
    }
    #endif
}

void get_wifi_passwd(GtkWidget *entry)
{
#ifndef __WIN32__
    gtk_spinner_start((GtkSpinner *)g_wifi_spinner);
    pid_t pid = fork();
    if (pid == 0)
    {
        M_DEBUG("execl...........");
        M_DEBUG("wifi name = %s", g_text_wifi_name);
        if (execl("/usr/bin/nmcli", "nmcli", "device", "wifi", "connect", g_text_wifi_name, "password", gtk_entry_get_text((GtkEntry *)entry), NULL) < 0)
        {
            exit(0);
        }
    }
    gtk_widget_destroy(g_sys_ask_window);
    g_wifi_connect_time ++;
    g_timeout_id_wifi_connect = g_timeout_add_seconds(2, (GSourceFunc)set_wifi_connect_label, NULL);
#endif
}

void nmcli_connection_show_print_list(pNode head)
{
    pNode p = head->next;
    if (p == NULL) {
        M_DEBUG("链表为空");
    } else {
        while (p != NULL) {
            p = p->next;
        }
    }
}

void nmcli_connection_show_free_list(pNode head)
{
    pNode pointer;
    int num = 0;

    while (NULL != head) {
        pointer = head;
        head = head->next;
        free(pointer);
        num ++;
    }
    M_DEBUG("free %d node", num);
}

pNode nmcli_connection_show_create_list(void)
{
    pNode head = (pNode)malloc(sizeof(Node));
    pNode tail = head;
    tail->next = NULL;
    const char *cmd = "nmcli connection show | grep -E -o '.{8}-.{4}-.{4}-.{4}-.{12}'";
    char buff[60];
    FILE *pf = popen(cmd, "r");
    while (1) {
        if (!fgets(buff, 60, pf)) {
            break;
        } else {
            pNode new_node = (pNode)malloc(sizeof(Node));
            buff[strlen(buff) - 1] = 0;
            strcpy(new_node->uuid, buff);
            tail->next = new_node;
            tail = new_node;
            new_node->next = NULL;
            memset(buff, 0, 60);
        }
    }
    pclose(pf);
    return head;
}

int nmcli_connection_show_get_active_num(void)
{
    int active_num = 0;
    const char *cmd = "nmcli connection show --active| grep -E -o '.{8}-.{4}-.{4}-.{4}-.{12}'";
    char buff[60];
    FILE *pf = popen(cmd, "r");
    while (1) {
        if (!fgets(buff, 60, pf)) {
            break;
        } else {
            active_num ++;
        }
    }
    pclose(pf);
    return  active_num;
}

void nmcli_connection_show_start_delete(pNode head, int active_num)
{
    pNode pointer = head->next;
    int i = 0;
    char cmd[200];
    for (i = 0; i < active_num; i++) {
        pointer = pointer->next;
    }
    while (pointer != NULL) {
        strcpy(cmd, "nmcli connection delete ");
        strcat(cmd, pointer->uuid);
        system(cmd);
        pointer = pointer->next;
        memset(cmd, 0, 200);
    }
}

void delete_nmcli_connection_show(void)
{
    int active_num = nmcli_connection_show_get_active_num();
    if (active_num == 0) {
        M_DEBUG("active_num == 0");
        return;
    }
    M_DEBUG("active_num = %d", active_num);

    pNode all_head = nmcli_connection_show_create_list();
    if (all_head->next == NULL) {
        M_DEBUG("all_head->next == NULL");
        free(all_head);
        return;
    }
    nmcli_connection_show_print_list(all_head);

    nmcli_connection_show_start_delete(all_head, active_num);

    nmcli_connection_show_free_list(all_head);
}

void g_btn_wifi_start_toggled(GtkToggleButton *togglebtn)
{
    if (gtk_toggle_button_get_active(togglebtn)) {
        set_css(g_btn_wifi_start, "button{ color:white;"
                "background:rgb(53,126,169);"
                "border:1px solid rgb(40,94,142);"
                "border-radius:0px}");
        set_css(g_btn_wifi_start, "button:hover{border-radius:0px;background: #3070a9}");
        gtk_button_set_label((GtkButton *)g_btn_wifi_start, "关闭");
        gtk_widget_set_tooltip_text((GtkWidget *)togglebtn, "关闭无线局域网");
        system("nmcli radio wifi on");
        M_DEBUG("nmcli radio wifi on");
        g_wifi_connect_time = 0;
        gdk_threads_add_idle((GSourceFunc)add_items, NULL);
        g_whether_wifi = 1;
    } else {
        delete_nmcli_connection_show();
        set_css(g_btn_wifi_start, "button{ color:white;"
                "background:#5cb85c;border:1px solid rgb(57,132,57);"
                "border-radius:0px}");
        set_css(g_btn_wifi_start, "button:hover{border-radius:0px;background:#449d44;}");
        gtk_button_set_label((GtkButton *)g_btn_wifi_start, "打开");
        gtk_widget_set_tooltip_text((GtkWidget *)togglebtn, "开启无线局域网");
        system("nmcli radio wifi off");
        M_DEBUG("nmcli radio wifi off");
        gtk_list_store_clear(GTK_LIST_STORE(g_items_model));
        if (g_timeout_id_wifi_connect) {
            g_source_remove(g_timeout_id_wifi_connect);
            g_timeout_id_wifi_connect = 0;
            gtk_spinner_stop((GtkSpinner *)g_wifi_spinner);
        }
        g_wifi_connect_time = 0;
        g_whether_wifi = 0;
    }
    set_wifi_connect_label();
}

GtkWidget *get_sys_wifi_info_fix(void)
{
    GtkWidget *fix_network_wifi = gtk_fixed_new();

    GtkWidget *scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwindow), GTK_SHADOW_ETCHED_IN);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    gtk_widget_set_size_request(scrolledwindow, 310, 273);
    GtkWidget *label_wifi = gtk_label_new("无线局域网");
    GtkWidget *label_g_wifi_name_now = gtk_label_new("当前连接状态:");
    g_wifi_spinner = gtk_spinner_new();
    g_btn_wifi_connect = gtk_button_new_with_label("连接");
    gtk_widget_set_size_request(g_btn_wifi_connect, 80, 1);
    gtk_widget_set_tooltip_text(g_btn_wifi_connect, "连接已选中的wifi");
    gtk_button_set_relief(GTK_BUTTON(g_btn_wifi_connect), GTK_RELIEF_NONE);
    g_btn_wifi_disconnect = gtk_button_new_with_label("断开");
    gtk_widget_set_tooltip_text(g_btn_wifi_disconnect, "断开当前已连接或者正在连接的wifi");
    gtk_widget_set_size_request(g_btn_wifi_disconnect, 80, 1);
    gtk_button_set_relief(GTK_BUTTON(g_btn_wifi_disconnect), GTK_RELIEF_NONE);
    g_btn_delete_connection_show = gtk_button_new_with_label("清除");
    gtk_widget_set_tooltip_text(g_btn_delete_connection_show, "清除已保存的wifi");
    gtk_widget_set_size_request(g_btn_delete_connection_show, 80, 1);
    gtk_button_set_relief(GTK_BUTTON(g_btn_delete_connection_show), GTK_RELIEF_NONE);
    set_wifi_state();
    g_wifi_name_now = gtk_label_new(NULL);
    if (g_whether_wifi) {
        g_btn_wifi_start = gtk_toggle_button_new_with_label("关闭");
        gtk_widget_set_tooltip_text(g_btn_wifi_start, "关闭无线局域网");
        gtk_widget_set_size_request(g_btn_wifi_start, 80, 1);
        gtk_button_set_relief(GTK_BUTTON(g_btn_wifi_start), GTK_RELIEF_NONE);
        gtk_toggle_button_set_active((GtkToggleButton *)g_btn_wifi_start, TRUE);
        set_css(g_btn_wifi_start, "button{ color:white;"
                "background:rgb(53,126,169);"
                "border:1px solid rgb(40,94,142);"
                "border-radius:0px}");
        set_css(g_btn_wifi_start, "button:hover{border-radius:0px;background: #3070a9}");
    } else {
        g_btn_wifi_start = gtk_toggle_button_new_with_label("打开");
        gtk_widget_set_tooltip_text(g_btn_wifi_start, "打开无线局域网");
        gtk_button_set_relief(GTK_BUTTON(g_btn_wifi_start), GTK_RELIEF_NONE);
        gtk_toggle_button_set_active((GtkToggleButton *)g_btn_wifi_start, FALSE);
        set_css(g_btn_wifi_start, "button{ color:white;"
                "background:#5cb85c;border:1px solid rgb(57,132,57);"
                "border-radius:0px}");
        set_css(g_btn_wifi_start, "button:hover{border-radius:0px;background:#449d44;}");
    }
    g_btn_wifi_research = gtk_button_new_with_label("刷新");
    gtk_widget_set_size_request(g_btn_wifi_research, 80, 1);
    gtk_widget_set_tooltip_text(g_btn_wifi_research, "刷新无线局域网");
    gtk_button_set_relief(GTK_BUTTON(g_btn_wifi_research), GTK_RELIEF_NONE);

    g_treeview_wifi = create_g_treeview_wifi();
    gtk_container_add(GTK_CONTAINER(scrolledwindow), g_treeview_wifi);
    gtk_fixed_put((GtkFixed *)fix_network_wifi, label_wifi,            55,  30);
    gtk_fixed_put((GtkFixed *)fix_network_wifi, label_g_wifi_name_now, 55,  66);
    gtk_fixed_put((GtkFixed *)fix_network_wifi, g_btn_wifi_start,      155, 18);
    gtk_fixed_put((GtkFixed *)fix_network_wifi, g_wifi_name_now,       185, 66);
    gtk_fixed_put((GtkFixed *)fix_network_wifi, scrolledwindow,        55,  97);
    gtk_fixed_put((GtkFixed *)fix_network_wifi, g_btn_wifi_research,   245, 18);
    gtk_fixed_put((GtkFixed *)fix_network_wifi, g_btn_delete_connection_show, 335, 18);
    gtk_fixed_put((GtkFixed *)fix_network_wifi, g_btn_wifi_connect,    375, 335);
    gtk_fixed_put((GtkFixed *)fix_network_wifi, g_btn_wifi_disconnect, 465, 335);
    gtk_fixed_put((GtkFixed *)fix_network_wifi, g_wifi_spinner,        165, 66);

    g_signal_connect(G_OBJECT(g_btn_wifi_start),      "toggled", G_CALLBACK(g_btn_wifi_start_toggled), NULL);
    g_signal_connect(G_OBJECT(g_btn_wifi_research),   "clicked", G_CALLBACK(add_items),                NULL);
    g_signal_connect(G_OBJECT(g_btn_wifi_connect),    "clicked", G_CALLBACK(wifi_connect),             NULL);
    g_signal_connect(G_OBJECT(g_btn_wifi_disconnect), "clicked", G_CALLBACK(wifi_disconnect),          NULL);
    g_signal_connect(G_OBJECT(g_btn_delete_connection_show), "clicked", G_CALLBACK(delete_nmcli_connection_show), NULL);
    set_css(g_btn_wifi_research, "button{ color:white;"
            "background:#5cb85c;border:1px solid rgb(57,132,57);"
            "border-radius:0px}");
    set_css(g_btn_wifi_research, "button:hover{border-radius:0px;background:#449d44;}");
    set_css(g_btn_delete_connection_show, "button{ color:white;"
            "background:#5cb85c;border:1px solid rgb(57,132,57);"
            "border-radius:0px}");
    set_css(g_btn_delete_connection_show, "button:hover{border-radius:0px;background:#449d44;}");
    set_css(g_btn_wifi_disconnect, "button{ color:white;"
            "background:#5cb85c;border:1px solid rgb(57,132,57);"
            "border-radius:0px}");
    set_css(g_btn_wifi_disconnect, "button:hover{border-radius:0px;background:#449d44;}");

    set_css(g_btn_wifi_connect, "button{ color:white;"
            "background:rgb(53,126,169);"
            "border:1px solid rgb(40,94,142);"
            "border-radius:0px}");
    set_css(g_btn_wifi_connect, "button:hover{border-radius:0px;background: #3070a9}");

    return fix_network_wifi;
}
